package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootChip;
import org.footballmanager.pandafootball.system.domain.FootLy;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.form.impl.FootLySearchForm;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootLyService;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.footballmanager.pandafootball.system.service.FootZnxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Service
public class FootLyServiceImpl extends ServiceImpl<FootLy> implements FootLyService {
    @Autowired
    private FootTeamService footTeamService;
    @Autowired
    private FootChipService footChipService;
    @Autowired
    private FootZnxService footZnxService;

    /**
     * 保存留言记录
     *
     * @param entity  参数
     * @param request 参数  @return 返回值
     */
    @Override
    @Transactional
    @SystemServiceLog(description = "保存留言记录")
    public int save(FootLy entity, HttpServletRequest request) {
        //管理员增加发放碎片，可以给用户增加不同碎片和钱，并发送留言
        if (entity.getMoney() != null && entity.getMoney() > 0D) {
            List<FootTeam> list = footTeamService.getall();
            for (FootTeam team : list) {
                team.setQdZzj(team.getQdZzj() + entity.getMoney());
                team.setYy("管理员发放奖励获得" + entity.getMoney() + "PB,当前球队的总资金为:" + team.getQdZzj() + "PB");
                footTeamService.updateNotNull(team, request);
            }
        }
        processChip(entity, request);
        if (request != null) {
            FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
            footZnxService.sendZnx(user.getId(), null, user.getNike() + "已经增加新公告，请前往查看",
                    null, "/index", request);
        }
        return super.save(entity, request);
    }

    /**
     * 处理碎片数据
     *
     * @param footLy  留言实体类
     * @param request 请求
     */
    private void processChip(@Valid FootLy footLy, HttpServletRequest request) {
        boolean flag1 = footLy.getBlueChip() > 0 || footLy.getGreenChip() > 0;
        boolean flag2 = footLy.getOrangeChip() > 0 || footLy.getPurpleChip() > 0;
        if (flag1 || flag2 || footLy.getWhiteChip() > 0) {
            List<FootChip> list = footChipService.getall();
            if (list != null && !list.isEmpty()) {
                for (FootChip chip : list) {
                    chip.setHaveBlue(chip.getHaveBlue() + footLy.getBlueChip());
                    chip.setHaveGreen(chip.getHaveGreen() + footLy.getGreenChip());
                    chip.setHaveOrange(chip.getHaveOrange() + footLy.getOrangeChip());
                    chip.setHavePurple(chip.getHavePurple() + footLy.getPurpleChip());
                    chip.setHaveWhile(chip.getHaveWhile() + footLy.getWhiteChip());
                    footChipService.updateNotNull(chip, request);
                }
            }
        }
    }

    /**
     * 根据发送时间降序获取留言
     *
     * @return 留言列表
     */
    @Override
    public List<FootLy> findLyBySendtimeDesc() {
        Example example = new Example(FootLy.class);
        example.createCriteria().andEqualTo("delFlag", 0);
        example.orderBy("sendTime").desc();
        return this.findall(example);
    }

    /**
     * 根据留言信息查找记录
     *
     * @param content 留言
     * @return 记录
     */
    @Override
    public FootLy findByContent(String content) {
        Example example = new Example(FootLy.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("content", content);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 查询留言
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footLySearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootLySearchForm footLySearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footLySearchForm);
        Page<FootLy> page = PageHelper.startPage(footLySearchForm.getPageIndex(), footLySearchForm.getPageSize());
        List<FootLy> list = this.findall(footLySearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footLySearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootLySearchForm footLySearchForm, HttpServletRequest request) {
        if (footLySearchForm.getSelectIds() != null) {
            String[] result = footLySearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

}
